package com.Pure;

import android.graphics.Point;
import android.view.MotionEvent;

import com.e3roid.drawable.*;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.event.ShapeEventListener;
import com.e3roid.E3Activity;
import com.e3roid.E3Scene;

enum PureMirrorDirection{
	MD_UPRIGHT,
	MD_DOWNRIGHT,
	MD_DOWNLEFT,
	MD_UPLEFT
}
public class PureMirror extends PureGameObject implements ShapeEventListener{
	
	// 1 = up/right, 7 = down/right, 9 = down/left, 3 = up/left
	//public int direction;
	private PureMirrorDirection direction;
	public float rotateAngle;
	public int originalX, originalY;
	public static boolean onClick = false; 
	
	public PureMirror(Sprite s)
	{
		super(s);
	
		direction = PureMirrorDirection.MD_UPRIGHT;
		rotateAngle = 0.0f;
		originalX = s.getRealX() + s.getWidth()/2;
		originalY = s.getRealY() + s.getHeight()/2;
		//System.out.println(s.getRealY() + "  " +  (s.getHeight()/2));
		s.addListener(this);
	}
	public PureMirrorDirection getDirection(){return direction;}
	public void setDirection(PureMirrorDirection d)
	{
		direction = d;
		switch(d)
		{
			case MD_UPLEFT:
				sprite.rotate(-90);
			break;
			case MD_DOWNRIGHT:
				sprite.rotate(90);
			break;
			case MD_DOWNLEFT:
				sprite.rotate(180);
			break;
			case MD_UPRIGHT:
				sprite.rotate(0);
			break;
			
		}
	}
	public void turnRight()
	{
		if(direction == PureMirrorDirection.MD_UPLEFT)
		{
			direction = PureMirrorDirection.MD_UPRIGHT;
		}else if(direction == PureMirrorDirection.MD_UPRIGHT)
		{
			direction = PureMirrorDirection.MD_DOWNRIGHT;
		}else if(direction == PureMirrorDirection.MD_DOWNRIGHT){
			direction = PureMirrorDirection.MD_DOWNLEFT;
		}else if(direction == PureMirrorDirection.MD_DOWNLEFT){
			direction = PureMirrorDirection.MD_UPLEFT;
		}
		setDirection(direction);
		
	}

	public static void setClick(boolean bool)
	{
		onClick = bool;
	}
	
	public static boolean getClick()
	{
			return onClick;
	}
	
	@Override
	public boolean onTouchEvent(E3Scene scene, Shape shape,
			MotionEvent motionEvent, int localX, int localY) {
		
		E3Activity act = PureMain.getInstance();
		//due to the performance this checking is not precise.
		PureGrid g = PureMain.getInstance().level.getGrid();
		PureLevel level = PureMain.getInstance().level;
		PureMain m = null;
		if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			originalX = sprite.getRealX() + sprite.getWidth()/2;
			originalY = sprite.getRealY() + sprite.getHeight()/2;
			//System.out.println("7 " + originalY);
			PureMain.currentObj = this;
//			rotateAngle += 90.0f;
//			sprite.rotate(rotateAngle);
			this.setClick(true);
		}
		
		if ( (motionEvent.getAction() == MotionEvent.ACTION_MOVE) && (this == PureMain.currentObj)) {
			sprite.move(act.getTouchEventX(scene, motionEvent) - (sprite.getWidth()/2), 
					act.getTouchEventY(scene, motionEvent) - (sprite.getHeight()/2));
		}
		
		int gSize = PureGrid.getGridSize();
		PureGameObject[][] container = g.getGameObjectList();
		
		//due to the performance this checking is not precise.
		if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
			int actualX = sprite.getRealX() + (sprite.getWidth()/2);
			int actualY = sprite.getRealY() + (sprite.getHeight()/2);
			if (actualY <= 64)
			{
				this.setClick(false);
				sprite.move(originalX - sprite.getWidth()/2, originalY - sprite.getHeight()/2);
			} else if (actualY > 384)
			{
				sprite.move(actualX - actualX % gSize , actualY - actualY % gSize);
				if(originalY < 384) {
					//putToStage(actualX, actualY, gSize);
					//g.addObjectFromItemList(this);
					putToItemList(actualX, actualY, gSize);
					g.removeObjectToItemList(this);
					System.out.println("1");
					//System.out.println(level.getVempireCount());
				} else if (	(originalX == actualX) &&
							(originalY == actualY))
						//if ((originalX == actualX - actualX % gSize) &&
						//	(originalY == actualY - actualY % gSize))
				{
					//if(this.getClick())
					//{
						rotateAngle += 90.0f;
						turnRight();
						System.out.println("2");
						//System.out.println(level.getVempireCount());
						
					//}
				} else {
					//if(g.isNullIndex(this)) {
						originalX = sprite.getRealX();
						originalY = sprite.getRealY();
						putToItemList(actualX, actualY, gSize);
						//g.updateIndex(this);
						System.out.println("3");
					//} else {
						//sprite.move(originalX - sprite.getWidth()/2, originalY - sprite.getHeight()/2);
						//System.out.println("333");
					//}
				}
				//System.out.println(level.getVempireCount());
				this.setClick(false);
			} else {
				sprite.move(actualX - actualX % gSize , actualY - actualY % gSize);
				System.out.println(g.isNullIndex(this));
				//sprite.move(originalX - sprite.getWidth()/2, originalY - sprite.getHeight()/2);
				
				if(originalY >= 384) {
					if(g.isNullIndex(this)) {
						putToStage(actualX, actualY, gSize);
						g.addObjectFromItemList(this);
						System.out.println("4");
						g.updateLightRay();
						//System.out.println(level.getVempireCount());
					} else {
						sprite.move(originalX - sprite.getWidth()/2, originalY - sprite.getHeight()/2);
						System.out.println(originalX + " " + actualX);
						System.out.println(originalY + " " + actualY);
						System.out.println("111");
					}
				} else if (	(originalX == actualX) &&
							(originalY == actualY))
				{
					if(this.getClick())
					{
						rotateAngle += 90.0f;
						System.out.println("5");
						turnRight();
						//setDirection(rotate)
						//sprite.rotate(rotateAngle);
						g.updateLightRay();
						System.out.println(originalX + " " + actualX);
						System.out.println(originalY + " " + actualY);
						//System.out.println(level.getVempireCount());
						
					}
				} else {
					if(g.isNullIndex(this)) {
						System.out.println("6 " + originalX + " " +  (actualX - actualX % gSize) + " " + originalY + " " +  actualY );
						originalX = sprite.getRealX();
						originalY = sprite.getRealY();
						g.updateIndex(this);
						g.updateLightRay();
						//System.out.println(level.getVempireCount());
					} else {
						sprite.move(originalX - sprite.getWidth()/2, originalY - sprite.getHeight()/2);
						System.out.println(originalX + " " + actualX);
						System.out.println(originalY + " " + actualY);
						System.out.println("222");
					}
				}
				//System.out.println(level.getVempireCount());
				this.setClick(false);
			}
			g.setUpdateLightRay(true);
			PureMain.currentObj = null;
		}
		return false;
	}

	public PureLightDirection getReflection(PureLightDirection lightDir)
	{
		PureLightDirection outDir = PureLightDirection.LD_NONE;
		
		if(lightDir == PureLightDirection.LD_RIGHT)
		{
			if(direction == PureMirrorDirection.MD_DOWNLEFT)
			{
				outDir = PureLightDirection.LD_DOWN;
			}else if(direction == PureMirrorDirection.MD_UPLEFT)
			{
				outDir = PureLightDirection.LD_UP;
			}
				
		}else if(lightDir == PureLightDirection.LD_LEFT)
		{
			if(direction == PureMirrorDirection.MD_DOWNRIGHT)
			{
				outDir = PureLightDirection.LD_DOWN;
			}else if(direction == PureMirrorDirection.MD_UPRIGHT)
			{
				outDir = PureLightDirection.LD_UP;
			}
				
		}else if(lightDir == PureLightDirection.LD_UP)
		{
			if(direction == PureMirrorDirection.MD_DOWNRIGHT)
			{
				outDir = PureLightDirection.LD_RIGHT;
			}else if(direction == PureMirrorDirection.MD_DOWNLEFT)
			{
				outDir = PureLightDirection.LD_LEFT;
			}
				
		}else if(lightDir == PureLightDirection.LD_DOWN)
		{
			if(direction == PureMirrorDirection.MD_UPLEFT)
			{
				outDir = PureLightDirection.LD_LEFT;
			}else if(direction == PureMirrorDirection.MD_UPRIGHT)
			{
				outDir = PureLightDirection.LD_RIGHT;
			}
				
		}
		
		
		return outDir;
	}
	
	@Override
	public PureLightDirection onLightCollide(PureGrid grid,Point lightFrom,Point lightEnd,
			int lengthOfLight,PureLightDirection lightFromDir)
	{
		//create a lightray from starting point to mirror
		grid.generateLightWay(lightFrom,lengthOfLight,lightFromDir);
		
		//update start position for reflection
		lightFrom.x = lightEnd.x;
		lightFrom.y = lightEnd.y;
		
		//calculate reflection direction
		return getReflection(lightFromDir);
		
		
	}
	public void putToItemList(int actualX, int actualY, int gSize)
	{
		sprite.move(actualX - actualX % gSize , actualY - actualY % gSize);
		originalX = sprite.getRealX();
		originalY = sprite.getRealY();
	}
	
	public void putToStage(int actualX, int actualY, int gSize)
	{
		sprite.move(actualX - actualX % gSize , actualY - actualY % gSize);
		originalX = sprite.getRealX();
		originalY = sprite.getRealY();
	}
	
}
